#include <bits/stdc++.h>
#define ll long long
using namespace std;

inline ll read(){
    ll x=0;char c=getchar();
    while (!isdigit(c))c=getchar();
    while (isdigit(c))x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x;
}

ll n,m,ta[200005],c[200005],a[200005],lg[200005],x[5],tn,k2;
char g[18][200005];
bool dp[18][200005],mx[18][200005];
struct qry{
    ll c,id;
}p[200005];
ll nwans,ans[200005];

void brute(ll pos){
    const ll tn=(1ll<<lg[pos+1]);
    nwans=0;
    for (ll i=0;i<=k2;i++)for (ll j=0;j<tn;j++)dp[i][j]=0,mx[i][j]=0;
    for (ll j=0;j<tn;j++){
        dp[k2][j]=1;
        mx[k2-1][j]|=dp[k2][j]&&(j>pos||a[j]<1);
    }

    for (ll i=k2-1;i>=k2-lg[pos+1];i--){
        for (ll j=0;j<tn;j++){
            ll rd=j>>(k2-i),pt=(j>>(k2-i-1)&1);
            ll tp=g[i][rd]-'0';
            if (pt){
                if (tp==0){
                    dp[i][j]=mx[i][rd<<1]&dp[i+1][j];
                }else{
                    dp[i][j]=dp[i+1][j]&(j>pos||a[j]>=k2-i);
                }
            }else{
                if (tp==0){
                    dp[i][j]=dp[i+1][j]&(j>pos||a[j]>=k2-i);
                }else{
                    dp[i][j]=mx[i][rd<<1|1]&dp[i+1][j];
                }
            }
            mx[i-1][rd]|=dp[i][j]&&(j>pos||(a[j]<k2-i+1));
        }
    }
    ll ps=k2-lg[pos+1];
    /*for (ll i=ps;i<=k2;i++){
        for (ll j=0;j<tn;j++){
            cout<<i<<","<<j<<","<<pos<<":"<<dp[i][j]<<"\n";
        }
    }
    cout<<"----------------\n";*/
    for (ll i=0;i<tn;i++){
        if (dp[ps][i])
        nwans+=i+1;
    }
}

int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin.tie(0);cout.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for (ll i=1;i<=n;i++)cin >> ta[i];
    ll fl=1;
    for (ll i=1;i<=m;i++){
        cin >> p[i].c;c[i]=p[i].c;
        p[i].id=i;
        fl&=!(p[i].c&(p[i].c-1));
    }
    sort(p+1,p+1+m,[](qry a,qry b){
        return a.c<b.c;
    });
    tn=1;
    while (tn<n)tn<<=1,k2++;
    for (ll i=1;i<=k2;i++){
        for (ll j=0;j<(1ll<<(k2-i));j++){
            cin >> g[k2-i][j];
        }
    }
    for (ll i=1;i<=n;i++){
        ll j=1,k=0;
        for (j=1;j<i;j<<=1,k++);
        lg[i]=k;
    }
    ll t;
    cin >> t;
    while (t--){
        for (ll i=0;i<4;i++)cin >> x[i];
        for (ll i=1;i<=n;i++)a[i-1]=ta[i]^x[i&3];
        for (ll i=0;i<=k2;i++)for (ll j=0;j<tn;j++)dp[i][j]=0,mx[i][j]=0;
        ll rk=0;
        nwans=0;
        for (ll j=0;j<n;j++){
            if (!fl)brute(j);
            else if (fl&&((j+1)&j)==0)brute(j);
            while (rk+1<=m&&p[rk+1].c==j+1){
                ans[p[rk+1].id]=nwans;
                rk++;
            }
        }
        ll rel=0;
        for (ll j=1;j<=m;j++){
                //cout<<c[j]<<":"<<ans[j]<<"\n";
            rel^=j*ans[j];
        }
        cout<< rel<<"\n";
    }
    return 0;
}
